# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dm_taking_timely_summary_hi_{int(time())}"
timeout = timedelta(minutes=60).seconds
doris_jms_dm__dm_taking_timely_summary_hi = RichSqlSensor(
    task_id='doris_jms_dm__dm_taking_timely_summary_hi',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris_uat',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_taking_timely_summary_hi PARTITION (
         p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+0) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+1) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace_uat }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_summary_hi/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace_uat }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_summary_hi/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace_uat }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_summary_hi/dt={{{{ execution_date | date_add(+0) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace_uat }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_summary_hi/dt={{{{ execution_date | date_add(+1) | cst_ds }}}}/*"
        )
        INTO TABLE dm_taking_timely_summary_hi
        FORMAT AS 'PARQUET'(
             sender_network_code
            ,sender_network_name
            ,sender_fran_code
            ,sender_fran_name
            ,sender_agent_code
            ,sender_agent_name
            ,sender_region_code
            ,sender_region_name
            ,sender_area_code
            ,sender_area_name
            ,sender_city_code
            ,sender_city_name
            ,sender_province_code
            ,sender_province_name
            ,sender_staff_code
            ,sender_staff_name
            ,customer_code
            ,customer_name
            ,order_source_code
            ,order_source_name
            ,order_source_group_code
            ,order_source_group_name
            ,diff_code
            ,diff_type
            ,diff_time
            ,wdd_cnt
            ,ydd_cnt
            ,wl_cnt
            ,yl_cnt
            ,cs_wl_cnt
            ,cs_yl_cnt
            ,taking_diff_cnt
            ,taking_miss_cnt
            ,all_cnt
            ,timely_cnt
            ,untimely_cnt
        )
        columns from path as (dt)
        set(
            sender_network_code      = sender_network_code
           ,sender_network_name      = sender_network_name
           ,sender_fran_code         = sender_fran_code
           ,sender_fran_name         = sender_fran_name
           ,sender_agent_code        = sender_agent_code
           ,sender_agent_name        = sender_agent_name
           ,sender_region_code       = sender_region_code
           ,sender_region_name       = sender_region_name
           ,sender_area_code         = sender_area_code
           ,sender_area_name         = sender_area_name
           ,sender_city_code         = sender_city_code
           ,sender_city_name         = sender_city_name
           ,sender_province_code     = sender_province_code
           ,sender_province_name     = sender_province_name
           ,sender_staff_code        = sender_staff_code
           ,sender_staff_name        = sender_staff_name
           ,customer_code            = customer_code
           ,customer_name            = customer_name
           ,order_source_code        = order_source_code
           ,order_source_name        = order_source_name
           ,order_source_group_code  = order_source_group_code
           ,order_source_group_name  = order_source_group_name
           ,diff_code                = diff_code
           ,diff_type                = diff_type
           ,diff_time                = diff_time
           ,wdd_cnt                  = wdd_cnt
           ,ydd_cnt                  = ydd_cnt
           ,wl_cnt                   = wl_cnt
           ,yl_cnt                   = yl_cnt
           ,cs_wl_cnt                = cs_wl_cnt
           ,cs_yl_cnt                = cs_yl_cnt
           ,taking_diff_cnt          = taking_diff_cnt
           ,taking_miss_cnt          = taking_miss_cnt
           ,all_cnt                  = all_cnt
           ,timely_cnt               = timely_cnt
           ,untimely_cnt             = untimely_cnt
           ,dt                       = dt
       )
    )
    WITH BROKER '{{{{ var.json.doris_brokers_uat | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )
